import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.sql.SQLException;


/**
 * The main window for the program
 * @author Bethany Sumner
 * @version 1.0
 * 
 */
public class MainWindow extends JFrame{
	
	/**
	 * Field currentUser.
	 */
	private String currentUser; // the username of the person logged in
	/**
	 * Field usertype.
	 */
	private int usertype; // the usertype of the current user (0-3)
	/**
	 * Field tab.
	 */
	private JTabbedPane tab;
	/**
	 * Field lblMessage.
	 */
	private JLabel lblMessage;
	/**
	 * Field messagePanel.
	 */
	private JPanel messagePanel;
	/**
	 * Field loginPanel.
	 */
	private LoginPanel loginPanel;
	/**
	 * Field sideBar.
	 */
	private SideBar sideBar;
	/**
	 * Field database.
	 */
	private Database database;
	/**
	 * Field logger.
	 */
	private Logs logger;
	
	/**
	 * MainWindow constructor
	 * @param database A reference to the database
	 */
	public MainWindow(Database database){
		
		this.database = database;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(1260,800));
		setTitle("Doctor's Office Support System");
		getContentPane().setLayout(new BorderLayout());
		
		// add message bar (to display error messages)
	    messagePanel = new JPanel();
	    messagePanel.setPreferredSize(new Dimension(800,30));
	    messagePanel.setBackground(Color.LIGHT_GRAY);
	    getContentPane().add(messagePanel, BorderLayout.NORTH);
	    lblMessage = new JLabel();
	    messagePanel.add(lblMessage);
	    
		resetGUI();
		
		
		try {
			logger = new Logs();
		} catch (SecurityException e) {
			showError("Error setting up logging");
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Sets up GUI for user that is not logged in
	 */
	private void resetGUI(){
		
		showMessage("Welcome to Clinic Quadrumvirate! Please log in.");
	    
	    // add login panel
	    loginPanel = new LoginPanel(this, database);
		getContentPane().add(loginPanel);
	    
	    setVisible(true);
		pack();
		
	}
	
	/**
	 * Adds panels to the tabbed pane depending on the usertype
	 * @param usertype Usertype of the user currently logged in
	 */
	private void addPanels(int usertype){
		
		// add Sidebar (for buttons)
		sideBar = new SideBar(this, usertype);
		getContentPane().add(sideBar, BorderLayout.WEST);
	
		// add tabbed pane (for windows)
	    tab = new JTabbedPane();
	    getContentPane().add(tab, BorderLayout.CENTER);
	    
	    // add the default panel for each usertype
	    switch(usertype){
		    case 0:	// system admin
		    	tab.add("View Users", new ViewUsers(this, database));
		    	break;
		    case 1: // doctor
		    	tab.add("View Appointments", new ViewAppointments(this, database));
		    	break;
		    case 2: // nurse
		    	tab.add("View Appointments", new ViewAppointments(this, database));
		    	break;
		    case 3: // patient
		    	tab.add("Request Appointment", new AddEditAppointment(this, database));
		    	break;
	    }
	    
	    pack();
	}
	
	/**
	 * Adds panel to the tabbed pane in the main window
	 * @param name The title to display on the tab
	 * @param panel The JPanel object to add
	 */
	public void addTab(String name, JPanel panel){
		tab.add(name, panel);
		tab.setSelectedComponent(panel);
	}
	
	/**
	 * Removes panel from the tabbed pane in the main window
	 * @param panel The JPanel object to remove
	 */
	public void removeTab(JPanel panel){
		tab.remove(panel);
	}
	
	/**
	 * Shows a message to the user on the top of the main window
	 * @param message The message to display
	 */
	public void showMessage(String message){
		messagePanel.setBackground(Color.LIGHT_GRAY);
		lblMessage.setText(message);
	}
	
	/**
	 * Shows an error message in red to the user on the top of the main window
	 * @param message The message to display
	 */
	public void showError(String message){
		messagePanel.setBackground(new Color(255, 204, 204));
		lblMessage.setText(message);
	}
	
	/**
	 * Method showAffirmation.
	 * @param message String
	 */
	public void showAffirmation(String message){
		messagePanel.setBackground(new Color(204, 255, 204));
		lblMessage.setText(message);
	}
	
	/**
	 * Makes the message to the user on the top bar blank
	 */
	public void clearMessage(){
		messagePanel.setBackground(Color.LIGHT_GRAY);
		showMessage("");
	}
	
	/**
	 * Called when a user logs in, sets up the GUI for them
	 * @param username The username of the user that just logged in
	 * @param usertype The type of user that just logged in (must be digit between 0-3)
	 */
	public void loginSuccessful(String username, int usertype){
		currentUser = username;
		this.usertype = usertype;
		showMessage("Welcome " + username);
		this.remove(loginPanel);
		addPanels(usertype);
	}
	
	/**
	 * Logs the user out, resets the GUI
	 */
	public void logout(){
		
		//reset user
		currentUser = "";
		usertype = -1;
		
		//reset gui
		this.remove(sideBar);
		this.remove(tab);
		resetGUI();
	}
	
	/**
	 * Returns user that is currently logged in
	 * @return The username of the user that is currently logged in */
	public String getCurrentUser(){
		return this.currentUser;
	}
	
	/**
	 * Returns the usertype of the user that is logged in
	 * @return The usertype (digits 1-3) of the user that is logged in (or -1 if no one is logged in) */
	public int getUsertype(){
		return this.usertype;
	}
	
	/**
	 * Gets the reference to the Database class
	 * @return The reference to the Database class */
	public Database getDatabase(){
		return this.database;
	}
	
	/**
	 * Logs error in text file
	 * @param e Exception
	 */
	public void logError(Exception e){
		logger.logError(e);
	}
	
	
	
	
	

}
